package ch.selise.exercise.pattern.notepad.command;

import java.util.ArrayList;
import java.util.List;

import ch.selise.exercise.pattern.notepad.command.impl.AddCommand;
import ch.selise.exercise.pattern.notepad.exception.EmptyStackException;

public class StackManipulator {
	private List<IUndoableCommand> commands = new ArrayList<IUndoableCommand>();
	private List<IUndoableCommand> undoCommands = new ArrayList<IUndoableCommand>();
	
	public void addCommand(IUndoableCommand command)
	{
		if(command instanceof AddCommand)
		{
			undoCommands.clear();
		}
		commands.add(command);
		
	}
	
	public void undoCommand() throws EmptyStackException
	{
		try {
			IUndoableCommand undo = commands.remove(commands.size() - 1);
			undo.undoCommand();
			//command.execute();
			undoCommands.add(undo);
			//return (IUndoableCommand) undo;
		} catch (Exception e) {
			throw new EmptyStackException("Command Stack is empty", e);
		}
	}
	
	public void redoCommand() throws EmptyStackException
	{
		try {
			IUndoableCommand redo = undoCommands.remove(undoCommands.size() - 1);
			redo.redoCommand();
			//command.execute();
			commands.add(redo);
			//return (IUndoableCommand) redo;
		} catch (Exception e) {
			throw new EmptyStackException("Undo Command Stack is empty", e);
		}
	}
}
